(ns samber.services.shop.address
  (:require
    [toucan.db :as db]
    [samber.db.models :as models]))


(defn address-list [user-id]
  (let [data (db/select models/ShopAddress :user_id user-id)]
    data))

(defn address-detail [user-id id]
  (let [data (first (db/select models/ShopAddress :user_id user-id :id id {:limit 1}))]))

(defn address-new [user-id address]
  ;; check default address & set to non default
  (if (= (:is_default 1))
    (db/update-where! models/ShopAddress {:is_default 0}
      {:user_id user-id :is_default 1}))

  ;; insert db
  (db/insert! models/ShopAddress (assoc address :user_id user-id)))

(defn address-delete [user-id id]
  (db/simple-delete! models/ShopAddress :user_id user-id :id id))
